package com.ruoyi.lm.controller;

import com.ruoyi.lm.domain.LmActivity;
import com.ruoyi.lm.domain.LmUser;
import com.ruoyi.lm.service.ILmActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.sun.org.apache.xalan.internal.xsltc.compiler.sym.error;

@Component("lmActivityTask")
public class LmActivityTask {
    @Autowired
    private ILmActivityService lmActivityService;
    public void updateStatus()
    {
        System.out.println("执行无参方法");
        //判断当前日期是否是截止日期，如果是截止日期，则终止活动
        LmActivity a = new LmActivity();
        //a.setActivityStatus("进行中");
        List<LmActivity> list = lmActivityService.selectLmActivityList(a);
        for(LmActivity temp :list){
            if(isSameDate(temp.getEndTime(),new Date())){
                //今天是截止日期
                LmActivity lmActivity = new LmActivity();
                lmActivity.setId(temp.getId());
                lmActivity.setActivityStatus("3");
                lmActivityService.updateLmActivity(lmActivity);
            }
        }
    }

    public static boolean isSameDate(Date date1, Date date2) {
        try {
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(date1);

            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(date2);

            boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                    .get(Calendar.YEAR);
            boolean isSameMonth = isSameYear
                    && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
            boolean isSameDate = isSameMonth
                    && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                    .get(Calendar.DAY_OF_MONTH);

            return isSameDate;
        } catch (Exception e) {
             System.out.println("[RatingEngine] error occurred: ERROR ");
        }
        return false;


    }
}
